package com.it.impl;

import com.it.ArticleService;
import com.it.dao.ArticleMapper;
import com.it.dao.FollowingMapper;
import com.it.dao.UserMapper;
import com.it.pojo.Article;
import com.it.pojo.User;
import com.it.pojo.params.article.ArticleQuery;
import com.it.pojo.vo.article.ArticleByUserIdVO;
import com.it.pojo.vo.article.ArticlePlateVO;
import com.it.pojo.vo.article.ArticleVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private FollowingMapper followingMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Integer saveArticle(Article article) {
        Date date = new Date();
        article.setGmtCreate(date);
        article.setGmtModified(date);
        return articleMapper.insert(article) > 0 ? article.getArticleId() : 0;
    }

    @Override
    public Map<String, Object> getArticletVO(Integer articleId) {
        ArticleVO articletVO = articleMapper.getArticletVO(articleId);
        Article article = articleMapper.selectById(articleId);
        article.setViews(article.getViews() + 1);
        Integer userId = articletVO.getUserId();
        Integer fanTotal = followingMapper.getFanTotal(userId);
        Integer followingTotal = followingMapper.getFollowingTotal(userId);
        User user = userMapper.selectById(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("article", articletVO);
        map.put("fanTotal", fanTotal);
        map.put("followingTotal", followingTotal);
        map.put("user", user);
        return map;
    }

    @Override
    public List<ArticlePlateVO> getArticleVOByPlateId(Integer plateId) {
        return articleMapper.getArticleVOByPlateId(plateId);

    }

    public Map<String, Object> listArticleIndexVO(Integer pageNum, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        Integer start = (pageNum - 1) * limit;
        List<ArticleByUserIdVO> articleVODesc = articleMapper.getArticleVODesc(start, limit);
        List<Article> article = articleMapper.listArticleHot();
        map.put("articleList", articleVODesc);
        map.put("hotArticleList", article);
        return map;


    }
}
